package com.oracle.zebrafreelytraveling.utils;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;

import com.iflytek.cloud.thirdparty.S;
import com.oracle.zebrafreelytraveling.App;
import com.oracle.zebrafreelytraveling.R;
import com.oracle.zebrafreelytraveling.data.model.IntroBase;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * Created by yydcnjjw on 2017/7/27.
 */

public class StringUtil {


    public static String getFileName(String uri){
        return uri.substring(uri.lastIndexOf('/')+1, uri.length());
    }

    /**
     * 获取拼音的首字母（大写）
     * @param pinyin
     * @return
     */
    public static String getFirstLetter(final String pinyin){
        if (TextUtils.isEmpty(pinyin)) return "定位";
        String c = pinyin.substring(0, 1);
        Pattern pattern = Pattern.compile("^[A-Za-z]+$");
        if (pattern.matcher(c).matches()){
            return c.toUpperCase();
        } else if ("0".equals(c)){
            return "定位";
        } else if ("1".equals(c)){
            return "热门";
        }
        return "定位";
    }

    /**
     * 提取出城市或者县
     * @param city
     * @param district
     * @return
     */
    public static String extractLocation(final String city, final String district){
        String str = district.contains("县") ? district.substring(0, district.length() - 1) : city.substring(0, city.length() - 1);
        return str;
    }

    public static String formatImageUrlList(List<String> imageUriList){
        StringBuilder build = new StringBuilder();
        for (int i = 0; i < imageUriList.size(); i++) {
            if (imageUriList.size() - 1 == i){
                build.append("image/cache/");
                build.append(getFileName(imageUriList.get(i)));
                break;
            }
            build.append("image/cache/");
            build.append(getFileName(imageUriList.get(i)));
            build.append(";");
        }
        return build.toString();
    }

    public static String formatLabelList(Set<Integer> labelList){
        Iterator<Integer> iterator = labelList.iterator();
        StringBuilder builder = new StringBuilder();
        while (iterator.hasNext()){
            builder.append(iterator.next());
            builder.append(";");
        }
        return builder.toString();
    }

    public static String[] formatLabelList(String label){
        if (label == null){
            return null;
        }
        String[] arrayResoure = App.getContextObject().getResources().getStringArray(R.array.classify_sort);
        String[] labelArray = label.split(";");
        String[] formatLabel = new String[labelArray.length];
        for (int i = 0; i < labelArray.length; i++) {
            if (labelArray[i].equals("0")){
                formatLabel[i] = arrayResoure[0];
            } else if (labelArray[i].equals("1")){
                formatLabel[i] = arrayResoure[1];
            } else if (labelArray[i].equals("2")){
                formatLabel[i] = arrayResoure[2];
            } else if (labelArray[i].equals("3")){
                formatLabel[i] = arrayResoure[3];
            }
        }
        return formatLabel;
    }

    public static List<String> formatImgList(String imageStr){
        String[] imgArray = imageStr.split(";");
        if (imgArray.length <= 0){
            return null;
        }
        List<String> imgList = new ArrayList<>();
        Collections.addAll(imgList, imgArray);
        return imgList;
    }


}
